package com.orange.base.component.biztool.simulateddata.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

/**
 * json工具类
 *
 * @author biankudingcha
 * @date: 2022-08-25
 * @since 0.5.0
 */
public final class JsonUtil {

    private static Gson gson = null;

    static {
        if (gson == null) {
            gson = new GsonBuilder().registerTypeAdapter(new TypeToken<Map<String, Object>>() {
            }.getType(), new MapTypeAdapter()).disableHtmlEscaping().create();
        }
    }

    private JsonUtil() {
    }

    /**
     * 将object对象转成json字符串
     * 
     * @param object
     * @return
     */
    public static String toJsonStr(Object object) {
        String gsonString = null;
        if (gson != null) {
            gsonString = gson.toJson(object);
        }
        return gsonString;
    }

    /**
     * 将gsonString转成泛型bean
     * 
     * @param gsonString
     * @param cls
     * @return
     */
    public static <T> T toBean(String jsonString, Class<T> cls) {
        T t = null;
        if (gson != null) {
            t = gson.fromJson(jsonString, cls);
        }
        return t;
    }

    /**
     * 转成list 解决泛型问题
     * 
     * @param jsonString
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> List<T> toList(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        if (gson != null) {
            JsonArray array = new JsonParser().parse(jsonString).getAsJsonArray();
            for (final JsonElement elem : array) {
                list.add(gson.fromJson(elem, cls));
            }
        }
        return list;
    }

    /**
     * 转成map的
     * 
     * @param jsonString
     * @return
     */
    public static Map<String, Object> toMap(String jsonString) {
        Map<String, Object> map = null;
        if (gson != null) {
            map = gson.fromJson(jsonString, new TypeToken<Map<String, Object>>() {
            }.getType());
        }
        return map;
    }

    /**
     * 转List<Map>
     * 
     * @param jsonString
     * @return
     */
    public static List<Map<String, Object>> toMapList(String jsonString) {
        List<Map<String, Object>> resultList = null;
        if (gson != null) {
            resultList = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() {
            }.getType());
        }
        return resultList;
    }

    /**
     * 扩展转Map方法
     * 
     * @author wlwang
     *
     */
    public static class MapTypeAdapter extends TypeAdapter<Object> {
        @Override
        public Object read(JsonReader in) throws IOException {
            JsonToken token = in.peek();
            switch (token) {
            case BEGIN_ARRAY:
                List<Object> list = new ArrayList<Object>();
                in.beginArray();
                while (in.hasNext()) {
                    list.add(read(in));
                }
                in.endArray();
                return list;

            case BEGIN_OBJECT:
                Map<String, Object> map = new LinkedTreeMap<String, Object>();
                in.beginObject();
                while (in.hasNext()) {
                    map.put(in.nextName(), read(in));
                }
                in.endObject();
                return map;

            case STRING:
                return in.nextString();

            case NUMBER:

                // 改写数字的处理逻辑，将数字值分为整型与浮点型。
                double dbNum = in.nextDouble();

                // 数字超过long的最大值，返回浮点类型
                if (dbNum > Long.MAX_VALUE) {
                    return dbNum;
                }

                // 判断数字是否为整数值
                long lngNum = (long) dbNum;
                if (dbNum == lngNum) {
                    return lngNum;
                } else {
                    return dbNum;
                }

            case BOOLEAN:
                return in.nextBoolean();

            case NULL:
                in.nextNull();
                return null;

            default:
                throw new IllegalStateException();
            }
        }

        @Override
        public void write(JsonWriter out, Object value) throws IOException {
            // 序列化无需实现
        }
    }

}
